// lib/services/auth_api.dart
import 'package:multi_shop/network/api_client.dart';

import '../network/api_routes.dart';
import 'package:get_storage/get_storage.dart';

class AuthApi {
  final _http = HttpClient();
  final _box = GetStorage();

  /// 注册
  Future<void> registerByEmail({
    required String email,
    required String password,
    required String code,
  }) async {
    await _http.postJson<void>(
      ApiRoutes.register, // 例如 /auth/register
      {'email': email, 'password': password, 'code': code},
    );
  }

  /// 登录
  Future<void> login({
    required String username,
    required String password,
  }) async {
    final data = await _http.postJson<Map<String, dynamic>>(ApiRoutes.login, {
      'account': username,
      'password': password,
    });
    final token = data['token'] as String? ?? '';
    final refreshToken = data['refreshToken'] as String? ?? '';
    _box.write('token', token);
    _box.write('refreshToken', refreshToken);
  }

  /// 发送验证码
  Future<void> sendEmailCode(String email, String scene) async {
    await _http.postJson<void>(ApiRoutes.getCode, {
      'account': email,
      'scene': scene,
    });
  }
}
